package cn.codesea.housework_user.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickWorkerListener;
import cn.codesea.housework_user.entity.Service;
import cn.codesea.housework_user.entity.Worker;
import cn.codesea.housework_user.ui.EvalutionListActivity;
import cn.codesea.housework_user.utils.DateUtil;

public class RYTJListRcvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;
    LayoutInflater mInflater;
    List<Worker> workers;


    //声明自定义的监听接口
    private OnRecyclerItemClickWorkerListener monItemClickListener;

    //提供set方法供Activity或Fragment调用
    public void setRecyclerItemClickListener(OnRecyclerItemClickWorkerListener listener){
        monItemClickListener=listener;
    }


    public RYTJListRcvAdapter(Context context, List<Worker> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        workers = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = mInflater.inflate(R.layout.item_recycler_rytjlist, parent, false);

        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof ItemViewHolder){
            Worker worker = workers.get(position);
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            itemViewHolder.name_tv.setText(worker.getName());
            itemViewHolder.phone_tv.setText(worker.getPhone());
            itemViewHolder.sex_tv.setText(worker.getSex());

            List<Service> services = worker.getServices();
            if (services != null){
                StringBuffer str = new StringBuffer();
                for (Service s : services){
                    str.append(s.getName() + "/");
                }
                str.deleteCharAt(str.length()-1);
                itemViewHolder.service_tv.setText(str.toString());
            }

            itemViewHolder.age_tv.setText(DateUtil.getAgeFromBirthTime(worker.getBirth())+"岁");// 年龄转化
            itemViewHolder.orderTime_tv.setText(worker.getOrder_time()+"次");
        }

    }

    @Override
    public int getItemCount() {
        return workers.size();
    }

    @Override
    public int getItemViewType(int position) {

        return super.getItemViewType(position);
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.cardView)
        CardView cardView;
        @BindView(R.id.command_tv)
        TextView command_tv;
        @BindView(R.id.checkbox)
        AppCompatCheckBox checkbox;

        @BindView(R.id.name_tv)
        TextView name_tv;
        @BindView(R.id.sex_tv)
        TextView sex_tv;
        @BindView(R.id.age_tv)
        TextView age_tv;
        @BindView(R.id.orderTime_tv)
        TextView orderTime_tv;
        @BindView(R.id.service_tv)
        TextView service_tv;
        @BindView(R.id.phone_tv)
        TextView phone_tv;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);

            checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked){
                            workers.get(getAdapterPosition()).setChecked(true);
                        }else {
                            workers.get(getAdapterPosition()).setChecked(false);
                        }
                    }
            });

            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (monItemClickListener!=null){
                        monItemClickListener.onItemClick(getAdapterPosition(),workers);
                        if (checkbox.isChecked()){
                            checkbox.setChecked(false);
                        }else {
                            checkbox.setChecked(true);
                        }
                    }
                }
            });
            command_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mContext.startActivity(new Intent(mContext,EvalutionListActivity.class).putExtra("title","历史评价:"+workers.get(getAdapterPosition()))
                            .putExtra("worker",workers.get(getAdapterPosition())));
                }
            });
        }

    }
}
